package com.hxexp.weixin.mvc.passenger.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hxexp.weixin.common.cache.PassengerCacheManager;
import com.hxexp.weixin.common.exception.APIException;
import com.hxexp.weixin.common.exception.ServiceException;
import com.hxexp.weixin.common.math.RandomUtil;
import com.hxexp.weixin.hpe.api.addpassenger.AddPassengerAPI;
import com.hxexp.weixin.hpe.api.addpassenger.model.AddPassengerParamBean;
import com.hxexp.weixin.hpe.api.addpassenger.model.AddPassengerResultBean;
import com.hxexp.weixin.hpe.api.delpassenger.DelPassengerAPI;
import com.hxexp.weixin.hpe.api.delpassenger.model.DelPassengerParamBean;
import com.hxexp.weixin.hpe.api.delpassenger.model.DelPassengerResultBean;
import com.hxexp.weixin.hpe.api.passengerlist.PassengerListAPI;
import com.hxexp.weixin.hpe.api.passengerlist.model.PassengerListParamBean;
import com.hxexp.weixin.hpe.api.passengerlist.model.PassengerListResultBean;
import com.hxexp.weixin.hpe.api.querypassenger.QueryPassengerAPI;
import com.hxexp.weixin.hpe.api.querypassenger.model.QueryPassengerParamBean;
import com.hxexp.weixin.hpe.api.querypassenger.model.QueryPassengerResultBean;
import com.hxexp.weixin.hpe.api.updatepassenger.UpdatePassengerAPI;
import com.hxexp.weixin.hpe.api.updatepassenger.model.UpdatePassengerParamBean;
import com.hxexp.weixin.hpe.api.updatepassenger.model.UpdatePassengerResultBean;
import com.hxexp.weixin.mvc.passenger.service.PassengerService;

/**
 * 常用乘车人服务实现类
 *
 * @author wang.bin
 * @author li.jiang
 */
@Service
public final class PassengerServiceImpl implements PassengerService {
    @Autowired
    private PassengerListAPI passengerListAPI;
    @Autowired
    private AddPassengerAPI addpassengerAPI;
    @Autowired
    private UpdatePassengerAPI updatePassenger;
    @Autowired
    private DelPassengerAPI delPassenger;
    @Autowired
    private QueryPassengerAPI queryPassengerAPI;
    @Autowired
    PassengerCacheManager passengersCacheManager;

    @Override
    public PassengerListResultBean getPassengerList(String userId) throws ServiceException {
        PassengerListParamBean passengerListParamBean = new PassengerListParamBean();
        PassengerListResultBean passengerListResultBean = new PassengerListResultBean();
        passengerListParamBean.setUserId(userId);

        try {
            passengerListResultBean = passengerListAPI.getPassengerList(passengerListParamBean);
        } catch (APIException e) {
            throw new ServiceException(e);
        }
        passengersCacheManager.batchPut(userId, passengerListResultBean.getPassengers());

        return passengerListResultBean;
    }
    @Override
    public AddPassengerResultBean addNewPassenger(AddPassengerParamBean addPassengerParamBean) throws ServiceException {

        AddPassengerResultBean addPassengerResultBean = new AddPassengerResultBean();

        try {
            String passengerId = RandomUtil.getUUID();
            addPassengerParamBean.setPassengerId(passengerId);
            addPassengerResultBean  = addpassengerAPI.addPassenger(addPassengerParamBean);
        } catch (APIException e) {
            throw new ServiceException(e);
        }
        return addPassengerResultBean;
    }
    @Override
    public UpdatePassengerResultBean updatePassenger(UpdatePassengerParamBean updatePassengerParamBean)
            throws ServiceException {
        UpdatePassengerResultBean updatePassengerResultBean = new UpdatePassengerResultBean();
        try{
            updatePassengerResultBean = updatePassenger.updatePassenger(updatePassengerParamBean);
        }catch(APIException e){
            throw new ServiceException(e);
        }
        return updatePassengerResultBean;
    }
    @Override
    public DelPassengerResultBean delPassenger(DelPassengerParamBean delPassengerParamBean) throws ServiceException {
        DelPassengerResultBean delPassengerResultBean = new DelPassengerResultBean();

        try{
            delPassengerResultBean = delPassenger.delPassenger(delPassengerParamBean);
        }catch(APIException e){
            throw new ServiceException(e);
        }
        return delPassengerResultBean;
    }
    @Override
    public QueryPassengerResultBean queryPassengerInfo(String passengerId) throws ServiceException {
        QueryPassengerParamBean queryPassengerParamBean = new QueryPassengerParamBean();
        QueryPassengerResultBean queryPassengerResultBean = new QueryPassengerResultBean();
        try{
            queryPassengerParamBean.setPassengerId(passengerId);
            queryPassengerResultBean = queryPassengerAPI.queryPassenger(queryPassengerParamBean);
        }catch(APIException e){
            throw new ServiceException(e);
        }
        return queryPassengerResultBean;
    }

}
